package com.demo.zhlg.service.impl;

import com.demo.zhlg.util.Result;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.demo.zhlg.entity.Wallets;
import com.demo.zhlg.mapper.WalletsMapper;
import com.demo.zhlg.service.WalletsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import static com.demo.zhlg.entity.table.WalletsTableDef.WALLETS;

/**
 *  服务层实现。
 *
 * @author lenovo
 * @since 2025-04-26
 */
@Service
public class WalletsServiceImpl extends ServiceImpl<WalletsMapper, Wallets> implements WalletsService {

    @Autowired
    private WalletsMapper walletsMapper;

    private int getUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        return loginUser.getUser().getUserId();
    }

    @Override
    public Object getUserBalance() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper
                .where(WALLETS.USER_ID.eq(getUserId()));
        return Result.success(walletsMapper.selectOneByQuery(queryWrapper));
    }
}
